// pages/danger/checkAccept/checkAccept.js
import Dialog from '../../../dist/dialog/dialog.js';
import Toast from '../../../dist/toast/toast.js';
var app = getApp()
var util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: app.globalData.baseUrl,

    inspectPublicId: 0,
    inspectPublic: {}, //检查单
    inspectList: [], //隐患清单
    reformSheet: {}, //整改单
    suggestion: '',
    taskId: '',
    result: 0, //0表示同意，1表示不同意



    selectLevel:"",
    hiddenModal: true,
    //changeExplain: false,
    inspectListPublicIndex: 0,
    inspectListPublic: {},
    deadline: "",
    inspectResult: "", //检查记录
    needChange: false,
    tempImagePaths: [],
    inspectImageIds: [],
    reformImagePaths: [],
    reformImageIds: [],
    changeExplain: "", //整改说明
    reformReply:'',//整改回执
    checkOpinion:'',//填入审核意见
    opinionActionSheet:false,
    actions:[
      { name:"同意签发"},
      { name:"退回修改"}
    ],
  },
  onClose() {
    this.setData({ show: false });
  },

  onSelect(event) {
    console.log(event.detail);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中....',
    })
    var that = this
    this.setData({
      inspectPublicId: options.inspectId,
      taskId: options.taskId
    })
    that.loadInspectPublicDetail(options.inspectId);
    that.loadReformSheet()
  },

  //加载整改单的信息
  loadReformSheet() {
    var that = this
    var header;
    var user = app.globalData.user
    header = {
      'Cookie': wx.getStorageSync("sessionid") //读取cookie
    };
    wx.request({
      url: app.globalData.baseUrl + '/safe/danger/reformSheet/find?dangerInspectPublicId=' +
        that.data.inspectPublicId + '&applyUserId=' + user.userId,
      header: header,
      method: "GET",
      success(res) {
        if (res.data.success && res.data.data.length > 0) {
          var reformSheet = res.data.data[0]
          that.setData({
            reformSheetId: reformSheet.id,
            reformReply: res.data.data[0].reformReply
          })
        }
      }
    })
  },


  /**
   * 加载发布的隐患排查的检查单
   */
  loadInspectPublicDetail(inspectPublicId) {

    var that = this
    var header;
    that.setData({
      inspectPublicId: inspectPublicId
    })
    header = {
      'Cookie': wx.getStorageSync("sessionid") //读取cookie
    };
    wx.request({
      url: app.globalData.baseUrl + '/safe/danger/inspectListPublic/find',
      header: header,
      method: "POST",
      data: {
        inspectPublicId: inspectPublicId,
        applyUserId: app.globalData.user.id,
        needChange: 1
      },
      success(res) {
        var inspectList = res.data.data
        for (var i = 0; i < inspectList.length; i++) {
          if (inspectList[i].deadline) {
            inspectList[i].deadlineStr = util.formatTime(new Date(inspectList[i].deadline))
          } else {
            inspectList[i].deadlineStr = "无"
          }
          if (!inspectList[i].hiddenDangerLevel) {
            inspectList[i].hiddenDangerLevel = "无"
          }
        }
        that.setData({
          inspectList: inspectList,
        })
      }
    })

    wx.request({
      url: app.globalData.baseUrl + '/safe/danger/inspectPublic/detail',
      data: {
        id: inspectPublicId
      },
      header: header,
      method: "POST",
      success(res) {
        if (res.data.success) {
          that.setData({
            inspectPublic: res.data.data
          })
        }
        wx.hideLoading()
      }
    })
  },
  /**
   * 审核意见change
   */
  checkOpinionChange(e){
    this.setData({
      checkOpinion:e.detail
    })
  },
  /**
   * 参考意见的弹出框
   */
  showOpinionTemplate(){
    this.setData({
      opinionActionSheet:true
    })
  },
  /**
   * 同意审核
   */
  agree() {
    var that = this
    Dialog.confirm({
      title: '提示',
      message: '确认通过审核',
      selector: '#van-dialog-doTask',

    }).then(() => {
      var header;

      header = {
        'Cookie': wx.getStorageSync("sessionid") //读取cookie
      };
      var params = {
        taskId: that.data.taskId,
        check: false,
        supervise:false,
        result: 0,
        comment: that.data.checkOpinion
      }
      wx.showLoading({
        title: '加载中....',
      })
      wx.request({
        url: app.globalData.baseUrl + '/safe/dangerFlow/doTask',
        header: header,
        method: "GET",
        data: params,
        success(res) {
          wx.hideLoading()
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }).catch(() => {

    });
  },
  /**
   * 驳回
   */
  reject() {
    var that = this
    Dialog.confirm({
      title: '提示',
      message: '审核不通过',
      selector: '#van-dialog-doTask',

    }).then(() => {
      var header;

      header = {
        'Cookie': wx.getStorageSync("sessionid") //读取cookie
      };
      var params = {
        taskId: that.data.taskId,
        supervise: false, //不需要督办
        result: 1,
      }
      wx.showLoading({
        title: '加载中....',
      })
      wx.request({
        url: app.globalData.baseUrl + '/safe/dangerFlow/doTask',
        header: header,
        method: "GET",
        data: params,
        success(res) {
          wx.hideLoading()
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }).catch(() => {

    });
  },

  /**
   * 显示记录隐患的窗口
   */
  showInspectModal(e) {
    var index = e.currentTarget.dataset.index
    var inspectPublicListId = e.currentTarget.dataset.inpsectpubliclistid
    var inspectPublicList = this.data.inspectList[index]
    this.setData({
      hiddenModal: false,
      changeExplain: inspectPublicList.changeExplain
    })
    this.setData({
      inspectListPublic: inspectPublicList,
      inspectListPublicIndex: index
    })
    this.initInspectListPublic(inspectPublicList)
  },
  /**
   * 点击记录，加载隐患记录的信息
   */
  initInspectListPublic(inspectPublicList) {
    var needChange = false;
    if (inspectPublicList.needChange == 0) {
      needChange = false
    } else {
      needChange = true
    }
    var deadline = ''
    if (inspectPublicList.deadline) {
      deadline = util.formatTime(new Date(inspectPublicList.deadline), 'Y/M/D')

    } else {
      deadline = util.formatTime(new Date())
    }
      let dangerPicList=[];
      inspectPublicList.dangerPics.forEach(function (item) {
          dangerPicList.push({
              fId:item.fId,
              url:app.globalData.fileUrl+item.path+'?x-oss-process=image/resize,m_fixed,h_70,w_70',
              oriUrl:app.globalData.fileUrl+item.path
          })
      });
      let changePicList=[];
      inspectPublicList.changePics.forEach(function (item) {
          changePicList.push({
              fId:item.fId,
              url:app.globalData.fileUrl+item.path+'?x-oss-process=image/resize,m_fixed,h_70,w_70',
              oriUrl:app.globalData.fileUrl+item.path
          })
      });
    this.setData({
      inspectResult: inspectPublicList.inspectResult,
      deadline: deadline,
      selectLevel: '一般(C级)',
      needChange: needChange,
      tempImagePaths: dangerPicList,
      reformImagePaths: changePicList,
    })
    if (inspectPublicList.hiddenDangerLevel) {
      this.setData({
        selectLevel: inspectPublicList.hiddenDangerLevel,
      })
    }
  },
  /**
     * 隐藏窗口
     */
  hiddenInspectModal() {
    this.setData({
      hiddenModal: true
    })
  },
    /**
     * 预览隐患照片
     */
    previewDangerImage(e) {
        var src = e.currentTarget.dataset.src;
        var imageList = new Array();
        for (var i = 0; i < this.data.tempImagePaths.length; i++) {
            imageList.push(this.data.tempImagePaths[i].oriUrl)
        }
        wx.previewImage({
            current: src,
            urls: imageList,
        })
    },
    /**
     * 预览整改照片照片
     */
    previewChangeImage(e){
        var src = e.currentTarget.dataset.src;
        var imageList = new Array();
        for (var i = 0; i < this.data.reformImagePaths.length; i++) {
            imageList.push(this.data.reformImagePaths[i].oriUrl)
        }
        wx.previewImage({
            current: src,
            urls: imageList,
        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})